package com.ihighest.app.infox.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.LableBindingAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 标签绑定界面
 * 
 * @author lkx Created at 2015-10-7 下午4:27:57
 * @version 1.0
 */
public class LableBindingActivity extends BaseActivity {
	private ActivityTitle title;
	private ListView lisView;
	private LableBindingAdapter adapter;
	public static int a;
	ArrayList<String> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		list = new ArrayList<String>();
		setContentView(R.layout.infox_activity_lablebinding);

		list = getIntent().getStringArrayListExtra(Const.STEPLIST);

		initView();

	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午2:49:38
	 */
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.lableBindingTitle);
		title.setTitle("我喜欢");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText(this.getString(R.string.advertisementLayout_jumpOver));

		lisView = (ListView) findViewById(R.id.lableListview);
		initData();
		lisView.setAdapter(adapter);
		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = null;

				if (list != null && list.size() != 0) {

					switch (list.get(0)) {
					case "tag":
						intent = new Intent(LableBindingActivity.this, LableBindingActivity.class);

						break;

					case "columnist":
						intent = new Intent(LableBindingActivity.this, SubscriptionColumnActivity.class);
						break;
					case "enterprise":
						intent = new Intent(LableBindingActivity.this, SubscriptionColumntwoActivity.class);
						break;

					}
					if (list.size() >= 1) {
						// 如果步骤很多，将步骤数据传给第一个界面
						list.remove(0);
						intent.putStringArrayListExtra(Const.STEPLIST, list);
					} else {
						intent = new Intent(LableBindingActivity.this, MainActivity.class);
					}
					startActivity(intent);
					finish();
				} else {
					intent = new Intent(LableBindingActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}

				// Intent intent = new Intent(LableBindingActivity.this,
				// SubscriptionColumnActivity.class);
				// startActivity(intent);
			}
		});
	}

	/**
	 * 获取标签数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午7:36:52
	 */
	private void initData() {
		if (CheckNetWork.isConnected(LableBindingActivity.this)) {
			// 如果网络正常
			Api.data("tag_recommend_list.json", new ApiCallback(this, true) {

				@Override
				public void handleSuccess(ApiResult result) {
					if (adapter == null) {
						adapter = new LableBindingAdapter(LableBindingActivity.this, result.getDatas());
					}
					lisView.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				}

				@Override
				public void handleFailure(Exception e, String msg) {
					// TODO Auto-generated method stub

				}
			}, this);

		}

	}
}
